import mysql.connector
import pandas as pd

# 连接到 MySQL 数据库
conn = mysql.connector.connect(
    host="localhost",
    user="root",
    password="password",
    database="mydatabase"
)

# 使用 pandas 执行 SQL 查询并导出到 CSV
query = "SELECT * FROM mytable"
df = pd.read_sql(query, conn)
df.to_csv("mytable.csv", index=False)

# 关闭连接
conn.close()
